#!/bin/sh
#
#   @Author: Gilgamesh Nootebos, Bas Toeter
# @Modified: 31-03-2009
#
# chkconfig: 2345 86 14
# description: OpenSSH server daemon

# The following settings are deferred to setenv.sh
# refer to $CATALINA_BASE/bin/setenv.sh
#
# JAVA_HOME=
# JAVA_OPTS=
# CATALINA_OPTS=

# This is an instance based tomcat install. This means that the binaries
# and application are separated into their own directory trees:
#
# CATALINA_HOME: tomcat binary install (tomcat only reads from this dir)
# CATALINA_BASE: application dir (bin/setenv.sh, conf, logs, temp, webapps, work)
#

: ${INSTANCE_NAME:=tomcat1}

: ${CATALINA_HOME:=/opt/apache-tomcat-6.0.18}
: ${CATALINA_BASE:=/data/$INSTANCE_NAME}

export CATALINA_HOME
export CATALINA_BASE

: ${TOMCAT_USER:=tomcat}
: ${TOMCAT_GROUP:=tomcat}

export TOMCAT_USER
export TOMCAT_GROUP

: ${TMP_DIR:=/var/tmp}

: ${CATALINA_PID:=/var/run/$INSTANCE_NAME.pid}
export CATALINA_PID

SU="su $TOMCAT_USER"
if [ "$LOGNAME" = "$TOMCAT_USER" ]; then
  SU="sh"
fi

usage () {
  echo "Usage $INSTANCE_NAME [start|stop|restart]"
  exit 1i
}

start () {
  echo "Starting $INSTANCE_NAME"
  touch $CATALINA_PID
  chown ${TOMCAT_USER}:${TOMCAT_GROUP} $CATALINA_PID
  chmod 640 $CATALINA_PID
  cd $CATALINA_BASE/logs
  $SU -c "$CATALINA_HOME/bin/startup.sh"
}

stop () {
  echo "Stopping $INSTANCE_NAME"
  $SU -c "$CATALINA_HOME/bin/shutdown.sh -force"
}

case "$1" in
  start)
    start
    ;;

  stop)
    stop
    ;;

  restart)
    stop
    start
    ;;

  *)
    ;;
esac
